#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#use strict;
BEGIN  {
    use File::Basename;      # - this gives us the path of the command
    ($::Bin) = dirname($0);  # - for this command we assume the modules and 
                             #     message maps are in the same place
    $::csmpm = $ENV{'CSM_PM'}  ? $ENV{'CSM_PM'} : '/opt/csm/pm';
}

# Look for perl modules first in the same directory as the command,
# then look in /opt/csm/pm.
use lib ($::Bin, $::csmpm);

use Getopt::Std;
use NodeUtils;
use InstallDefs;
use InstallUtils;

$ENV{'CT_MANAGEMENT_SCOPE'} = 1;# set local scope because we only want classes on the mgmt svr
$ENV{'CT_SESSION_SCOPE'} = 1;   # todo: remove when lsrsrc-api converts to above
$::MSGCAT = 'csmInstall.cat';

# Look for the message map file first in the same directory as the command,
# then look in /opt/csm/msgmaps.
if (-f "$::Bin/csmInstall.csminstall.map")
{
    $::MSGMAPPATH = $ENV{'CSM_MSGMAPS'} ? $ENV{'CSM_MSGMAPS'} : $::Bin;
}
else
{
    $::MSGMAPPATH = $ENV{'CSM_MSGMAPS'} ? $ENV{'CSM_MSGMAPS'} : '/opt/csm/msgmaps';
}
$::MSGSET = 'csminstall';


sub process_bootLoader
{
    # Process the /etc/lilo.conf or the /etc/grub.conf file
    # Add the following two lines to the file either after the "default=" line
    # or, if a "default=" line doesn't exist, add the lines before the first 
    # "image=" line.
    $bootLoader	   = shift;
    $bootLoaderbak = $bootLoader . ".bak";
    $addline1	= "serial = 1,9600n8";
    $addline2	= "append = \"console=tty1 console=ttyS1,9600\"";

    # If the lines were not already in the file, add them in the correct place.
    unless(-f $bootLoader){
    	return 1;
    }
    NodeUtils->runcmd("/bin/cp -p $bootLoader $bootLoaderbak");  # Back it up first

    unless(open (BLBAK, "<$bootLoaderbak")){
    	NodeUtils->message('W', 'EMsgCANT_OPEN_FOR_CONSOLE', $bootLoaderbak);
	return;
	    #|| die "Cannot open $lilobak to add remote console lines";
    }
    unless(open (BOOTLOADER, ">$bootLoader")){
    	NodeUtils->message('W', 'EMsgCANT_OPEN_FOR_CONSOLE', $bootLoaderbak);
	return;
	    #|| die "Cannot open $lilo to add remote console lines";
    }

    $added_lines ="";
    $line = "";
    while (<BLBAK>)
    {
	$line = $_;
	if ($line =~ /default\s*=/)
	{
	    NodeUtils->message('V','IMsgADDING_LINE', $bootLoader, $addline1);
	    NodeUtils->message('V','IMsgADDING_LINE', $bootLoader, $addline2);
	    print BOOTLOADER $line;
	    print BOOTLOADER "$addline1\n";
	    print BOOTLOADER "$addline2\n";
	    $added_lines = "true";
	}
	elsif (($line =~ /image\s*=/) && (! $added_lines))
	{
	    NodeUtils->message('V','IMsgADDING_LINE', $bootLoader, $addline1);
	    NodeUtils->message('V','IMsgADDING_LINE', $bootLoader, $addline2);
	    print BOOTLOADER "$addline1\n";
	    print BOOTLOADER "$addline2\n";
	    print BOOTLOADER $line;
	    $added_lines = "true";
	}
	elsif (($line =~ /$addline1/) || ($line =~ /$addline2/))
	{
	    NodeUtils->message('V','IMsgREMOVING_LINE', $bootLoader, $line);
	}
	else
	{
	    print BOOTLOADER $line;
	}
    }

    close (BOOTLOADER);
    close (BLBAK);
}


sub process_inittab
{
    # Process the /etc/inittab file
    # Here is the old cfengine code
    #BeginGroupIfNoLineContaining "s1:345:respawn:/sbin/agetty 9600 ttyS1 xterm"
    #    Append ""
    #    Append "# CSM RPM post installation added this entry"
    #    Append "# to allow serial console support for this node"
    #    Append "s1:345:respawn:/sbin/agetty 9600 ttyS1 xterm"
    #EndGroup

    $inittab	= "/etc/inittab";
    $addlines	= "
# CSM RPM post installation added this entry
# to allow serial console support for this node
s1:345:respawn:/sbin/agetty 9600 ttyS1 xterm
";
    $searchline = "s1:345:respawn:/sbin/agetty 9600 ttyS1 xterm";
    $output = `/bin/grep '$searchline' $inittab`;
    if ($output)
    {
	#print "Found lines in $inittab.  Nothing to do\n";
	return 0;
    }

    unless(open (INITTAB, ">>$inittab")){
            NodeUtils->message('E1', 'EMsgCANT_OPEN_FOR_CONSOLE',$inittab);
	    #|| die "Cannot open $inittab to add remote console lines";
    }

    print INITTAB $addlines;

    close(INITTAB);
}

$VERBOSE = ($ARGV[0] eq "-v") || ($ARGV[0] eq "-V");
&process_bootLoader('/etc/lilo.conf');
&process_bootLoader('/etc/grub.conf');
&process_inittab();
